import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.zone.ZoneRules;
import java.util.Date;


public class CalTime {
    public CalTime() {
    }
    public String classify(String beginTime, String endTime) throws ParseException {
        String flag = null;
        double money = 0.0;
        int bit = 0; //标志位，0表示情况a,1表示情况b,2表示情况c
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //时区转换
        ZonedDateTime begin = ZonedDateTime.of(LocalDateTime.parse(beginTime, ofPattern), ZoneId.of("America/Chicago"));
        ZonedDateTime end = ZonedDateTime.of(LocalDateTime.parse(endTime, ofPattern), ZoneId.of("America/Chicago"));
        ZoneId zoneId = begin.getZone();
        ZoneRules rules = zoneId.getRules();
        //判断是否是夏令时
        boolean beginTiming = rules.isDaylightSavings(begin.toInstant());
        boolean endTiming = rules.isDaylightSavings(end.toInstant());
        //因为java内部在对有时区的时间相减时会自动考虑时区问题，这里把时间转为没有时区划分的本地时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String noBegin = begin.format(ofPattern);
        String noEnd = end.format(ofPattern);
        Date beginTrue = format.parse(noBegin);
        Date endTrue = format.parse(noEnd);

        /*a、通话开始时间和结束时间都在夏令时,或者都在冬令时*/
        if (beginTiming == endTiming) {
            money = getPrice(beginTrue, endTrue, bit);
            flag = "通话在同一时区电费为" + money;
        }
        /*b、通话从夏令时跨越到冬令时*/
        if (beginTiming && !endTiming) {
            bit = 1;
            money = getPrice(beginTrue, endTrue, bit);
            flag = "通话从夏令时跨越到冬令时电费为" + money;
        }
        /*c、通话从冬令时跨越到夏令时*/
        if (!beginTiming && endTiming) {
            bit = 2;
            money = getPrice(beginTrue, endTrue, bit);
            flag = "通话从冬令时跨越到夏令时电费为" + money;
        }
        //通话时长不超过30小时
        if (money == -1.0){
            return "通话时长过长";
        }
        //通话时间错误
        if (money == -2) {
            return "通话时间有误";
        }
        return flag;
    }

    /*计算最终的通话价费用*/
    public double getPrice(Date beginTime, Date endTime, int bit) throws ParseException {
        double price;
        long diff = endTime.getTime() - beginTime.getTime();//这样得到的差值是毫秒级别
        if (diff < 0){
            return -2;
        }
        //通话出错
        //通话从夏令时跨越到冬令时
        if (bit == 1) {
            diff = diff + 1000 * 60 * 60;
        }
        //通话从冬令时跨越到夏令时
        if (bit == 2) {
            diff = diff - 1000 * 60 * 60;
        }
        double flag = diff / (1000 * 60.0);
        int trueFlag = (int)diff / (1000 * 60);
        //时长不足一分钟按一分钟算
        if (flag > trueFlag) {
            trueFlag++;
        }
        //System.out.println(trueFlag);
       if (trueFlag <= 20.0){
           price = trueFlag*0.05;
       }else if (trueFlag >= 20&&trueFlag <= 1800){
           trueFlag = trueFlag-20;
           price = 1 +trueFlag*0.1;
       }else {
           price = -1.0;
       }
       return price;
    }
}
